package com.wzy.authority.controller;

import com.github.pagehelper.Page;
import com.wzy.authority.base.dto.SysRoleDto;
import com.wzy.authority.base.request.PageParameter;
import com.wzy.authority.base.result.Results;
import com.wzy.authority.domain.SysRole;
import com.wzy.authority.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: authorityManagement
 * @description: 角色控制器
 * @author: 1
 * @create: 2020-03-14 20:02
 **/
@Slf4j
@Controller
@RequestMapping("/role")
public class RoleController {

    @Resource
    private SysRoleService sysRoleServiceImpl;


    @ResponseBody
    @GetMapping("/list")
    public Results<Page<SysRole>> getAllRoll(PageParameter pageParameter, String roleName) {
        log.info("/role/list  的参数是:" + pageParameter.toString() + " --- " + roleName);
        return sysRoleServiceImpl.getRoleListByPage(pageParameter, roleName);
    }

    @ResponseBody
    @GetMapping("/all")
    public Results<List<SysRole>> getAllRoleList() {
        return sysRoleServiceImpl.getAllRoleList();
    }


    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("sysRole", new SysRole());
        return "role/role-add";
    }


    @PostMapping("/save")
    @ResponseBody
    public Results<Void> saveRole(@RequestBody SysRoleDto sysRoleDto) {
        log.info("接口:role/save的参数是:" + sysRoleDto.toString());
        return sysRoleServiceImpl.addRole(sysRoleDto);
    }


    @ResponseBody
    @PostMapping("/update")
    public Results<Void> updateRole(@RequestBody SysRoleDto sysRoleDto) {
        log.info("接口/update:的参数为" + sysRoleDto.toString() + "-----" + sysRoleDto.getId());
        return sysRoleServiceImpl.updateRole(sysRoleDto);
    }


    @GetMapping("edit/{id}")
    public String selectSysRoleById(Model model, @PathVariable Integer id) {
        model.addAttribute("sysRole", sysRoleServiceImpl.selectSysRoleById(id));
        return "role/role-edit";
    }


    @ResponseBody
    @GetMapping("/delete")
    public Results<Void> deleteRole(Integer id){
        return sysRoleServiceImpl.deleteRole(id);
    }
}
